package org.yn.shopping.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.PageInfo;
import org.yn.shopping.domain.ShoppingCart;
import org.yn.shopping.dto.ShoppingCartDTO;
import org.yn.shopping.dto.StateDTO;
import org.yn.shopping.query.ShoppingCartQuery;
import org.yn.shopping.service.IShoppingCartService;
import org.yn.shopping.vo.ShoppingCartVO;

import javax.validation.Valid;
import java.util.List;

@PermissionAnnotation(name="shoppingCart管理",descs = "shoppingCart管理")
@Api("shoppingCart管理接口")
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    public IShoppingCartService shoppingCartService;


    /**
     * 保存和修改公用的
     * @param shoppingCart 传递的实体
     * @return Ajaxresult转换结果
     */
    @PermissionAnnotation(name = "保存和修改ShoppingCart", descs = "保存和修改ShoppingCart")
    @ApiOperation(value = "保存和修改")
    @PostMapping
    public AjaxResult updateOrInsert(@RequestBody ShoppingCart shoppingCart) {
        if ( shoppingCart.getId() != null)
            shoppingCartService.update(shoppingCart);
            else
            shoppingCartService.insert(shoppingCart);
        return AjaxResult.success();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @PermissionAnnotation(name = "删除ShoppingCart", descs = "删除ShoppingCart")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
            shoppingCartService.delete(id);
        return AjaxResult.success();
    }

    /**
    * 根据CarId删除对象信息
    * @param carId
    * @return
    */
    @PermissionAnnotation(name = "根据CarId删除对象信息", descs = "根据CarId删除对象信息")
    @ApiOperation(value = "根据CarId删除对象信息")
    @DeleteMapping(value = "/deleteByCarId/{carId}")
    public AjaxResult deleteByCarId(@PathVariable("carId") Long carId) {
            shoppingCartService.deleteByCarId(carId);
        return AjaxResult.success();
    }

    /**
    * 根据CarId删除对象信息
    * @param userId
    * @return
    */
    @PermissionAnnotation(name = "清空购物车", descs = "清空购物车")
    @ApiOperation(value = "清空购物车")
    @DeleteMapping(value = "/deleteAllByUserId/{userId}")
    public AjaxResult deleteAllByUserId(@PathVariable("userId") Long userId) {
            shoppingCartService.deleteAllByUserId(userId);
        return AjaxResult.success();
    }

    //获取用户
    @PermissionAnnotation(name = "获取单个ShoppingCart", descs = "获取单个ShoppingCart")
    @ApiOperation(value = "获取单个ShoppingCart")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id) {
        ShoppingCart shoppingCart =shoppingCartService.selectById(id);
        return AjaxResult.success(shoppingCart);
    }


    /**
    * 查看所有的XX信息
    * @return
    */
    @PermissionAnnotation(name = "获取所有ShoppingCart", descs = "获取所有ShoppingCart")
    @ApiOperation(value = "获取所有ShoppingCart")
    @GetMapping
    public AjaxResult selectAll() {

        List< ShoppingCart> list = shoppingCartService.selectAll();
        return AjaxResult.success(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageInfo 分页对象
    */
    @PermissionAnnotation(name = "分页查询ShoppingCart", descs = "分页查询ShoppingCart")
    @ApiOperation(value = "分页查询ShoppingCart")
    @PutMapping
    public AjaxResult queryPage(@RequestBody ShoppingCartQuery query) {
        PageInfo<ShoppingCart> pageInfo = shoppingCartService.queryPage(query);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PermissionAnnotation(name = "批量删除ShoppingCart", descs = "批量删除ShoppingCart")
    @ApiOperation(value = "批量删除ShoppingCart")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
            shoppingCartService.batchDelete(ids);
        return AjaxResult.success();
    }


    /**
     * 购物车新增
     * @param dto
     * @return
     */
    @PermissionAnnotation(name = "购物车新增", descs = "购物车新增")
    @ApiOperation(value = "购物车新增")
    @PostMapping ("/add")
    public AjaxResult addShoppingCart(@Valid @RequestBody ShoppingCartDTO dto) {
        shoppingCartService.addShoppingCart(dto);
        return AjaxResult.success();
    }

    /**
     * 根据userId获取购物车List
     * @param userId
     * @return
     */
    @PermissionAnnotation(name = "根据userid获取购物车", descs = "根据userid获取购物车")
    @ApiOperation(value = "根据userid获取购物车")
    @PostMapping ("/getShoppingCart/{userId}")
    public AjaxResult getShoppingCart(@PathVariable("userId") Long userId) {
        List<List<ShoppingCartVO>> shoppingCartVOList=  shoppingCartService.getShoppingCartByUserId(userId);
        return AjaxResult.success(shoppingCartVOList);
    }


    /**
     * 根据userId修改购物车单个的勾选状态
     * @param dto
     * @return
     */
    @PermissionAnnotation(name = "根据userid获取购物车", descs = "根据userid获取购物车")
    @ApiOperation(value = "根据userid获取购物车")
    @PostMapping ("/chageState")
    public AjaxResult chageState(@Valid @RequestBody StateDTO dto) {
        shoppingCartService.chageState(dto);
    return AjaxResult.success();
    }

    @GetMapping ("/count/{userId}")
    public AjaxResult chageState(@PathVariable("userId") Long userId) {
       Integer count= shoppingCartService.count(userId);
        return AjaxResult.success(count);
    }



}